<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { loginByQQ, bindMobile } from '@/services/user'
import { useSendMobileCode } from '@/composables'
import { mobileRules, codeRules } from '@/utils/rules'
import type { User } from '@/types/user'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
const openId = ref('')
const isNeedBind = ref(false)
const store = useUserStore()
const router = useRouter()
onMounted(() => {
  if (window.QC.Login.check()) {
    window.QC.Login.getMe((id) => {
      console.log(id)
      openId.value = id
      loginByQQ(id)
        .then((res) => {
          // 登录成功
          console.log('登录成功')
          loginSuccess(res)
        })
        .catch(() => {
          // 修改isNeedBind为true 去绑定手机
          isNeedBind.value = true
        })
    })
  }
})

// 绑定手机
const mobile = ref('')
const code = ref('')
const { form, time, send } = useSendMobileCode(mobile, 'bindMobile')
const bind = async () => {
  const res = await bindMobile({
    mobile: mobile.value,
    code: code.value,
    openId: openId.value
  })
  loginSuccess(res)
}

const loginSuccess = (res: { data: User }) => {
  store.setUser(res.data)
  // 如果有回跳地址就跳转回跳地址
  router.push(store.returnUrl || '/user')
  showToast('登录成功')
  store.setReturnUrl('')
}
</script>

<template>
  <div class="login-page" v-if="isNeedBind">
    <cp-nav-bar title="绑定手机"></cp-nav-bar>
    <div class="login-head">
      <h3>手机绑定</h3>
    </div>
    <van-form autocomplete="off" ref="form" @submit="bind">
      <van-field
        v-model="mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="mobileRules"
      ></van-field>
      <van-field
        v-model="code"
        :rules="codeRules"
        name="code"
        placeholder="请输入验证码"
      >
        <template #button
          ><span class="btn-send" :class="{ active: time > 0 }" @click="send">{{
            time > 0 ? `${time}s后再次发送` : '发送验证码'
          }}</span></template
        >
      </van-field>
      <div class="cp-cell">
        <van-button
          style="margin-top: 50px"
          block
          round
          type="primary"
          native-type="submit"
        >
          立即绑定
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
